package com.reservationserver.service.impl.admin;

import com.reservationpojo.domin.po.Getmgrpo;
import com.reservationpojo.domin.vo.Addmgrvo;
import com.reservationpojo.domin.vo.Getmgrvo;
import com.reservationpojo.entity.Mgr;
import com.reservationpojo.utils.Jwtutils;
import com.reservationserver.mapper.admin.Mgrmapper;
import com.reservationserver.service.admin.Mgrservice;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Math.min;

@Slf4j
@Service
@RequiredArgsConstructor
public class Mgrserviceimpl implements Mgrservice {

    private final Mgrmapper mgrmapper;
    private RedisTemplate<String, String> redisTemplate;
    @Override
    public void addmgr(Addmgrvo mgrvo) {
        mgrvo.setInitial(true);
        mgrmapper.addmgr(mgrvo);
    }

    @Override
    public Getmgrpo getmgr(Getmgrvo getmgrvo) {
        int page= (getmgrvo.getPageNumber()-1)*10;
        getmgrvo.setTot(page);
        List<Mgr> mgrs=mgrmapper.getmgrmapper(getmgrvo);
        if(mgrs==null){
            return null;
        }
        Getmgrpo getmgrpo=new Getmgrpo();
        getmgrpo.setMgrList(mgrs);
        return getmgrpo;
    }

    @Override
    public String mgrlogin(Mgr mgr) {
        Map<String,Object> claims=new HashMap<>();
        claims.put("code","1");
        claims.put("mid",mgr.getMgrId());
        return Jwtutils.generateJwt(claims);
    }

    @Override
    public void upmgr(Mgr mgr) {
        if(mgr.getPassword()!=null&&mgr.getPassword()!=""){
            try {
                redisTemplate.delete(mgr.getMgrId());
            }catch (Exception e){
                log.info("redis空了");
                mgrmapper.upmgr(mgr);
            }

        }


    }

    @Override
    public void deletemgr(String mgrId) {

        try {
            redisTemplate.delete(mgrId);
        }catch (Exception e){
            log.info("redis没有数据");
            mgrmapper.deletemgr(mgrId);
        }


    }
}
